#   LibAut
#       Figure 5 
#   Matthew C. Wilson, Juraj Medzihorsky
#   2021-08-10

library(pBrackets)
library(parallel)
options(mc.cores=3)
options(stringsAsFactors=F)
source('helpers.R')

#   sessionInfo()
##  R version 3.6.3 (2020-02-29)
##  Platform: x86_64-pc-linux-gnu (64-bit)
##  Running under: Ubuntu 16.04.7 LTS
##
##  Matrix products: default
##  BLAS/LAPACK: /opt/OpenBLAS/lib/libopenblas_haswellp-r0.3.9.so
##
##  locale:
##   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
##   [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_GB.UTF-8       LC_NAME=C
##   [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
##  attached base packages:
##  [1] parallel  stats     graphics  grDevices utils     datasets  methods   base
##
##  other attached packages:
##  [1] pBrackets_1.0  nvimcom_0.9-28 colorout_1.2-0
##
##  loaded via a namespace (and not attached):
##  [1] compiler_3.6.3 tools_3.6.3    grid_3.6.3


#-------------------------------------------------------------------------------

code_dir <- getwd()
data_dir <- gsub('code$', 'data', code_dir)
figs_dir <- gsub('code$', 'figures', code_dir)


setwd(data_dir)
vdem <- readRDS('V-Dem-CY-Full+Others-v10.rds')
ert <- read.csv('eps.csv') # episodes with V10 and ERT 2.0
#lib <- read.csv('libaut.csv')
setwd(figs_dir)

#------------------------------------------------------------------------------

vdem <- subset(vdem, year>=1899)

usa <- vdem[(vdem$year>=1900)&(vdem$country_text_id%in%'USA'),
            c('v2x_polyarchy', 'e_polity2', 'year')]

min(vdem$e_polity2, na.rm=T)

pfun <- function(i) (i+10)/20

usa$polity <- pfun(usa$e_polity2)

l4 <- c(-10, -5, 0, 5, 10)

sgray <- gray(0.3)
pgray <- natcol('purple')

cy <- which(usa$year==1960)
ee <- which((usa$year>1912)&(usa$year<1922))
mcex <- 1-gr(-3)

pdf('Figure_5_alt.pdf', 6, 6*0.59)
#
par(mar=c(1.5,3,1.5,3), las=1)
plot.new()
plot.window(xlim=c(1900, 2020), ylim=c(0,1))
pu <- par('usr')
sw <- strwidth('M')
#
rect(1913, pu[3], 1921, pu[4], border=grey(1,0.0), col=natcol('blue', 25), lwd=1e-2)
brackets(1913, pu[4], 1921, pu[4], xpd=T, h=+2e-2, col=natcol('blue')) 
text((1913+1921)/2, pu[4], 'successful episode',
     col=natcol('blue'), pos=3, offset=0.5, cex=mcex, font=1, xpd=T)
a1 <- axTicks(1)
a2 <- axTicks(2)
a4 <- pfun(l4)
axis(1, labels=F, tck=-gr(1)*1e-2, lwd=gr(-2), lwd.ticks=gr(-2))
axis(1, lwd=0, col=sgray, line=-gr(-1))
axis(2, labels=F, tck=-gr(1)*1e-2, col=sgray, lwd=gr(-1), lwd.ticks=gr(-1))
axis(2, lwd=0, col=sgray, col.axis=sgray, line=-gr(-2))
text(pu[1]-sw*3.7, mean(a4), 'Electoral Democracy Index', col=sgray, srt=90, xpd=T)
axis(4, a4, labels=F, tck=-gr(1)*1e-2, col=pgray, lwd=gr(-1), lwd.ticks=gr(-1))
axis(4, a4, l4, lwd=0, col=pgray, col.axis=pgray, line=-gr(-2))
text(pu[2]+sw*3.7, mean(a4), 'Polity IV Index', col=pgray, srt=270, xpd=T)
lines(usa$year, usa$polity, col=pgray, lty='21', lend='round')
lines(usa$year, usa$v2x_polyarchy, col=sgray, lwd=gr(-1))
lines(usa$year[ee], usa$v2x_polyarchy[ee], col=natcol('blue'))
text(1960, usa$v2x_polyarchy[cy], 'EDI', col=sgray, cex=mcex, font=1, offset=0.7, pos=3)
text(1959, usa$polity[cy], 'Polity IV', col=pgray, cex=mcex, font=1, offset=0.3, pos=3, xpd=T)
#
dev.off()


##  pdf('Figure_5_alt2.pdf', 6, 6*0.59)
##  #
##  par(mar=c(1.5,3,1.5,3), las=1)
##  plot.new()
##  plot.window(xlim=c(1900, 2020), ylim=c(0,1))
##  pu <- par('usr')
##  sh <- strheight('M')
##  sw <- strwidth('M')
##  #
##  pm <- min(usa$v2x_polyarchy[ee], na.rm=T) - sh*gr(+1)
##  lines(rep(1913, 2), c(pm, usa$v2x_polyarchy[ee[1]]-sh*gr(-2)), col=natcol('blue'), lwd=gr(-1), lty='11') 
##  lines(rep(1921, 2), c(pm, usa$v2x_polyarchy[rev(ee)[1]]-sh*gr(-0)), col=natcol('blue'), lwd=gr(-1), lty='11') 
##  brackets(1913, pm, 1921, pm, xpd=T, h=-2e-2, col=natcol('blue'), lwd=gr(-1), lty='11') 
##  text((1913+1921)/2, pm, 'successful\nepisode',
##       col=natcol('blue'), pos=1, offset=0.75, cex=mcex, font=1, xpd=T)
##  a1 <- axTicks(1)
##  a2 <- axTicks(2)
##  a4 <- pfun(l4)
##  axis(1, labels=F, tck=-gr(1)*1e-2, lwd=gr(-2), lwd.ticks=gr(-2))
##  axis(1, lwd=0, col=sgray, line=-gr(-1))
##  axis(2, labels=F, tck=-gr(1)*1e-2, col=sgray, lwd=gr(-1), lwd.ticks=gr(-1))
##  axis(2, lwd=0, col=sgray, col.axis=sgray, line=-gr(-2))
##  text(pu[1]-sw*3.7, mean(a4), 'Electoral Democracy Index', col=sgray, srt=90, xpd=T)
##  axis(4, a4, labels=F, tck=-gr(1)*1e-2, col=pgray, lwd=gr(-1), lwd.ticks=gr(-1))
##  axis(4, a4, l4, lwd=0, col=pgray, col.axis=pgray, line=-gr(-2))
##  text(pu[2]+sw*3.7, mean(a4), 'Polity IV Index', col=pgray, srt=270, xpd=T)
##  lines(usa$year, usa$polity, col=pgray, lty='21', lend='round')
##  lines(usa$year, usa$v2x_polyarchy, col=sgray, lwd=gr(-1))
##  lines(usa$year[ee], usa$v2x_polyarchy[ee], col=natcol('blue'))
##  text(1960, usa$v2x_polyarchy[cy], 'EDI', col=sgray, cex=mcex, font=1, offset=0.7, pos=3)
##  text(1959, usa$polity[cy], 'Polity IV', col=pgray, cex=mcex, font=1, offset=0.3, pos=3, xpd=T)
##  #
##  dev.off()

#   SCRIPT END